package com.openlogic.common.logic.utils;

import java.security.MessageDigest;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openlogic.common.core.exception.UtilException;

/**
 * @ClassName: MD5Util
 * @Description: MD5加密工具类
 * @author weiyongpeng
 * @date 2024年11月9日 上午7:39:22
 */
public class MD5Util {

	private static Logger log = LoggerFactory.getLogger(MD5Util.class);

	public static void main(String[] args) throws InterruptedException {
		Object[] arr = new Object[] { "dbName" };
		Object[] objects = Arrays.copyOf(arr, arr.length + 2);
		System.out.println(objects.length);
		int length = arr.length;
		objects[length] = 1;
		objects[length + 1] = 2;
		log.info("{}", Arrays.toString(objects));
		String encrypt = MD5Util.encrypt("sql" + ":" + Arrays.toString(arr));
		log.info("encrypt:{}", encrypt);
	}

	private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
			'f' };

	/**
	 * 描述：MD5加密
	 * @Title: encrypt
	 * @param value
	 * @return
	 * @author weiyongpeng
	 * @date 2024年11月9日 上午7:43:26
	 */
	public static String encrypt(String value) {
		return encrypt(value.getBytes());
	}

	/**
	 * 描述：MD5加密
	 * @Title: encrypt
	 * @param value
	 * @return
	 * @author weiyongpeng
	 * @date 2024年11月9日 上午7:43:37
	 */
	public static String encrypt(byte[] value) {
		try {
			byte[] bytes = MessageDigest.getInstance("MD5").digest(value);
			char[] chars = new char[32];
			for (int i = 0; i < chars.length; i = i + 2) {
				byte b = bytes[i / 2];
				chars[i] = HEX_CHARS[(b >>> 0x4) & 0xf];
				chars[i + 1] = HEX_CHARS[b & 0xf];
			}
			return new String(chars);
		} catch (Exception e) {
			throw new UtilException("md5 encrypt error", e);
		}
	}
}
